package lpl.com.xyd.test.manage.common;

import io.undertow.UndertowOptions;
import io.undertow.servlet.api.SecurityConstraint;
import io.undertow.servlet.api.SecurityInfo;
import io.undertow.servlet.api.TransportGuaranteeType;
import io.undertow.servlet.api.WebResourceCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Author mingliang
 * @Date 2018-08-02 16:57
 */
//@Configuration
public class Http2Config {

    private final static Logger LOGGER = LoggerFactory.getLogger(Http2Config.class);

    /**
     * undertow服务器下http重定向到https
     * @return
     */
    @Bean
    public UndertowServletWebServerFactory undertowServletWebServerFactory() {
        LOGGER.info("--------------------------{}",UndertowServletWebServerFactory.class);
        UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
        factory.addBuilderCustomizers(builder ->{
                builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true)
                        .setServerOption(UndertowOptions.HTTP2_SETTINGS_ENABLE_PUSH,true)
                .addHttpListener(8085,"0.0.0.0");
        });

        //下面这段是将http的8080端口重定向到https的8443端口上
//        factory.addDeploymentInfoCustomizers(deploymentInfo -> {
//            deploymentInfo.addSecurityConstraint(new SecurityConstraint()
//                    .addWebResourceCollection(new WebResourceCollection()
//                            .addUrlPattern("/*")) .setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL)
//                    .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT))
//                    .setConfidentialPortManager(exchange -> 8443);
//        });
        return factory;
    }
}
